//
//  SearchViewController.m
//  A_Comic
//
//  Created by sos on 16/1/23.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "SearchViewController.h"
#import <AFNetworking.h>
#import "WMEFirstPageModel.h"
//#import "NSArray+ShareInstance.h"
#import "WMEFirstPageInformationVC.h"
//#import "SearchModel.h"
@interface SearchViewController ()<UISearchBarDelegate, UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) UISearchBar *searchBar;
@property (nonatomic,retain) NSMutableArray *dataArr;
@property (nonatomic,retain) WMEFirstPageModel *str;
@property (nonatomic,retain) UILabel *label;
@end

@implementation SearchViewController
- (void)dealloc
{
    [_tableView release];
    [_searchBar release];
    [_dataArr release];
    [_str release];
    [_label release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [NSMutableArray array];
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //创建
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height-64 - 49) style:UITableViewStylePlain];
    //签订代理人
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.hidden = YES;
    [self.tableView release];
    
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    self.searchBar.placeholder = @"请输入要搜索的内容";
//    searchBar.showsCancelButton = YES;
    self.searchBar.delegate = self;
    
    self.searchBar.clearsContextBeforeDrawing = YES;
//    self.tableView.tableHeaderView = searchBar;
//    [self.view addSubview:self.searchBar];
    
    self.navigationItem.titleView = _searchBar;
    
//        [self data];

    
    
    
    
}
-(void)data
{
//    NSMutableArray *sousuoDataArr = [NSArray shareDefuld];
    
//    NSLog(@"~~~~~~~~~~%@",sousuoDataArr);
//    for (NSDictionary *dic in sousuoDataArr) {
//    self.dataArr = [HomePageModel modelHandlerWithArray:sousuoDataArr];
//    self.dataArr = sousuoDataArr;
//    self.dataArr = [HomePageModel modelHandlerWithArray:sousuoDataArr];


}


//协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    WMEFirstPageModel *m = self.dataArr[indexPath.row];
    //范围性关键字高亮
    NSString *data = m.msg;
    [data rangeOfString:m.msg];
    NSRange range = [data rangeOfString:self.searchBar.text];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:m.msg];

    [attr setAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} range:range];
//
    cell.textLabel.attributedText = attr;
//    [attr release];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WMEFirstPageInformationVC *deta = [[WMEFirstPageInformationVC alloc]init];
    WMEFirstPageModel *m = self.dataArr[indexPath.row];
    deta.jiekou = m.ID.stringValue;

    
    [self.navigationController pushViewController:deta animated:YES];
    
}


-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //获得searchbar中用户输入的内容
    NSString *str = self.searchBar.text;
    [_label removeFromSuperview];
    UILabel *huo = [self.view viewWithTag:5002];
    [huo removeFromSuperview];
//    NSString *url = [strOne stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //根据转码后的字符串 拼接网址 进行网络请求
    NSString *urlStr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:str]];

    // 拼接
    NSString *url =[@"http://203.80.144.212/napi/blog/list/by_search/?include_fields=sender%2Calbum%2Cicon_url%2Creply_count%2Clike_count&platform_version=4.4.4&device_platform=HM%2B2A&__dtac=%257B%2522_r%2522%253A%2520%2522350505%2522%257D&screen_width=720&screen_height=1280&__domain=www.duitang.com&app_version=68&kw=叶子&start=0&platform_name=Android&locale=zh&app_code=nayutas" stringByReplacingOccurrencesOfString:@"kw=叶子" withString:[NSString stringWithFormat:@"kw=%@", urlStr]];
    NSLog(@"%@", url);
    
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        NSLog(@"%@", responseObject);
        
        NSArray *arr = responseObject[@"data"][@"object_list"];
        self.dataArr = [WMEFirstPageModel modelHandlerWithArray:arr];
        [_tableView reloadData];
        if (_dataArr.count == 0) {
            UILabel *kong = [[UILabel alloc] initWithFrame:self.view.bounds];
            kong.backgroundColor = [UIColor whiteColor];
            kong.text = @"搜不到";
            kong.textAlignment = NSTextAlignmentCenter;
            kong.tag = 5002;
            [self.view addSubview:kong];
            [kong release];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
    

    self.tableView.hidden = NO;
    [self.tableView reloadData];
    [self.searchBar resignFirstResponder];
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.dataArr removeAllObjects];
    self.searchBar.text = nil;
    [self.tableView reloadData];
    if (![_searchBar.text length]) {
        self.label = [[UILabel alloc] initWithFrame:self.view.frame];
        _label.text = @"请,你还未输入哦";
        _label.backgroundColor = [UIColor whiteColor];
        _label.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_label];
        [_label release];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
